package noobteam.deartrainer.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author 高乾威
 * 2021-6-23 19：30 用户注册功能
 */
@Mapper
public interface UserInfoMapper {

    /**
     * 注册功能，向用户信息表（usersinfo）插入用户名和密码
     */
    @Insert("insert into usersinfo (userName, userPassword, userSignDate) " +
            "values (#{js.userName}, #{js.userPassword}, #{js.userSignDate})")
    public boolean insertName(@Param("js") Map<String,String> map);

    /**
     * 注册时查询重复用户名
     */
    @Select("SELECT userName FROM usersinfo")
    public List<Map<String,Object>> selectName();

    /**
     * 查询用户信息，用于显示在用户信息管理页面上
     */
    @Select("SELECT u.userId ,u.userImage ,u.userName ,u.userNickname ,u.userAge ," +
            "u.userAccountBalance ,u.userPhone ,u.userSex ,u.userSignDate \n" +
            "FROM deartrainer.usersinfo u;")
    public List<Map<String,Object>> findAll();

    /**
     * 通过 用户Id 查询用户信息
     */
    @Select("SELECT u.userImage ,u.userName ,u.userNickname ,u.userAge ," +
            "u.userAccountBalance ,u.userPhone ,u.userSex ,u.userSignDate \n" +
            "FROM deartrainer.usersinfo u\n" +
            "WHERE userId = #{id};")
    public Map<String,Object> findUserById(int id);


    @Select("SELECT u.userId, u.userImage ,u.userNickname ,u.userAge ," +
            "u.userAccountBalance ,u.userPhone ,u.userSex ,u.userSignDate \n" +
            "FROM usersinfo u\n" +
            "WHERE userName = #{userName}")
    public Map<String,Object> findUserByName(String userName);

    /**
     * 查询已有密码
     * 传入此用户的 name
     * 返回List为现在的密码
     * 用户登录的时候使用
     */
    @Select("SELECT userPassword FROM usersinfo WHERE userName = #{userName}")
    public Map<String,Object> selectPasswordByName(String userName);

    /**
     * 修改信息
     * 返回boolean反映修改是否完成
     */
    @Update("UPDATE usersinfo SET userNickname = #{js.userNickname}, " +
            "userSex = #{js.userSex}, userAge = #{js.userAge}, userPhone = #{js.userPhone} " +
            "WHERE userId = #{js.userId}")
    public boolean updatePersonalInfo(@Param("js") Map<String,String> map);

    /**
     * 查询已有密码
     * 传入此用户的id
     * 返回现在的密码
     */
    @Select("SELECT userPassword FROM usersinfo WHERE userId = #{userId}")
    public Map<String,Object> selectPassword(@Param("userId") Integer userId);


    /**
     * 修改密码
     * 传入新密码、用户
     * 返回修改结果boolean，反映修改是否完成
     */
    @Update("UPDATE usersinfo SET userPassword = #{js.newPassword} " +
            "WHERE userName = #{js.userName}")
    public boolean updatePassword(@Param("js") Map<String,String> map);

    /**
     * 删除用户
     */
    @Delete("DELETE FROM usersinfo WHERE userId = #{userId}")
    public boolean deleteUser(@Param("userId") Integer userId);

    /**
     * 更新用户头像
     * @param map
     * @return
     */
    @Update("UPDATE usersinfo SET userImage = #{js.userImage} " +
            "WHERE userId = #{js.userId}")
    boolean updatePhoto(@Param("js") Map<String, String> map);

    @Update("UPDATE usersinfo SET userAccountBalance = #{price} + userAccountBalance WHERE userId = #{userId}")
    boolean addBalance(@Param("userId") Integer userId, @Param("price") Double price);


    /**
     * 获取账户余额
     */
    @Select("SELECT userAccountBalance FROM usersinfo WHERE userId = #{userId}")
    public double getAccount(@Param("userId") int userId);

    /**
     * 购买课程
     * 余额减少
     */
    @Update("UPDATE usersinfo SET userAccountBalance = userAccountBalance - #{map.price} WHERE userId = #{map.userId}")
    public boolean buyCourse(@Param("map") Map<String, Object> map);

    /**
     * 充值
     */
    @Update("UPDATE usersinfo SET userAccountBalance = userAccountBalance + #{map.money} WHERE userId = #{map.userId}")
    public boolean addMoney(@Param("map") Map<String, String> map);


}
